{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Overview\n",
    "\n",
    "This is the first step for data preparation.\n",
    "\n",
    "Window images, as well as reindex them \n",
    "\n",
    "Input: original CT images\n",
    "\n",
    "Output: Images with abdominal windowing\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once deleted, variables cannot be recovered. Proceed (y/[n])? y\n"
     ]
    }
   ],
   "source": [
    "%reset\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "import numpy as np\n",
    "import os\n",
    "import glob\n",
    "import SimpleITK as sitk\n",
    "\n",
    "import sys\n",
    "\n",
    "sys.path.insert(0, '../../../dataloaders/')\n",
    "import niftiio as nio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set up directories for images\n",
    "IMG_FOLDER=\"./CT/img/\"\n",
    "SEG_FOLDER=\"./CT/label/\"\n",
    "OUT_FOLDER=\"./tmp_normalized/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "imgs = glob.glob(IMG_FOLDER + \"/*.nii.gz\")\n",
    "imgs = [ fid for fid in sorted(imgs) ]\n",
    "segs = [ fid for fid in sorted(glob.glob(SEG_FOLDER + \"/*.nii.gz\")) ]\n",
    "\n",
    "pids = [   pid.split(\"img0\")[-1].split(\".\")[0] for pid in imgs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# helper function\n",
    "def copy_spacing_ori(src, dst):\n",
    "    dst.SetSpacing(src.GetSpacing())\n",
    "    dst.SetOrigin(src.GetOrigin())\n",
    "    dst.SetDirection(src.GetDirection())\n",
    "    return dst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./tmp_normalized/image_0.nii.gz has been save\n",
      "./tmp_normalized/label_0.nii.gz has been save\n",
      "./tmp_normalized/image_1.nii.gz has been save\n",
      "./tmp_normalized/label_1.nii.gz has been save\n",
      "./tmp_normalized/image_2.nii.gz has been save\n",
      "./tmp_normalized/label_2.nii.gz has been save\n",
      "./tmp_normalized/image_3.nii.gz has been save\n",
      "./tmp_normalized/label_3.nii.gz has been save\n",
      "./tmp_normalized/image_4.nii.gz has been save\n",
      "./tmp_normalized/label_4.nii.gz has been save\n",
      "./tmp_normalized/image_5.nii.gz has been save\n",
      "./tmp_normalized/label_5.nii.gz has been save\n",
      "./tmp_normalized/image_6.nii.gz has been save\n",
      "./tmp_normalized/label_6.nii.gz has been save\n",
      "./tmp_normalized/image_7.nii.gz has been save\n",
      "./tmp_normalized/label_7.nii.gz has been save\n",
      "./tmp_normalized/image_8.nii.gz has been save\n",
      "./tmp_normalized/label_8.nii.gz has been save\n",
      "./tmp_normalized/image_9.nii.gz has been save\n",
      "./tmp_normalized/label_9.nii.gz has been save\n",
      "./tmp_normalized/image_10.nii.gz has been save\n",
      "./tmp_normalized/label_10.nii.gz has been save\n",
      "./tmp_normalized/image_11.nii.gz has been save\n",
      "./tmp_normalized/label_11.nii.gz has been save\n",
      "./tmp_normalized/image_12.nii.gz has been save\n",
      "./tmp_normalized/label_12.nii.gz has been save\n",
      "./tmp_normalized/image_13.nii.gz has been save\n",
      "./tmp_normalized/label_13.nii.gz has been save\n",
      "./tmp_normalized/image_14.nii.gz has been save\n",
      "./tmp_normalized/label_14.nii.gz has been save\n",
      "./tmp_normalized/image_15.nii.gz has been save\n",
      "./tmp_normalized/label_15.nii.gz has been save\n",
      "./tmp_normalized/image_16.nii.gz has been save\n",
      "./tmp_normalized/label_16.nii.gz has been save\n",
      "./tmp_normalized/image_17.nii.gz has been save\n",
      "./tmp_normalized/label_17.nii.gz has been save\n",
      "./tmp_normalized/image_18.nii.gz has been save\n",
      "./tmp_normalized/label_18.nii.gz has been save\n",
      "./tmp_normalized/image_19.nii.gz has been save\n",
      "./tmp_normalized/label_19.nii.gz has been save\n",
      "./tmp_normalized/image_20.nii.gz has been save\n",
      "./tmp_normalized/label_20.nii.gz has been save\n",
      "./tmp_normalized/image_21.nii.gz has been save\n",
      "./tmp_normalized/label_21.nii.gz has been save\n",
      "./tmp_normalized/image_22.nii.gz has been save\n",
      "./tmp_normalized/label_22.nii.gz has been save\n",
      "./tmp_normalized/image_23.nii.gz has been save\n",
      "./tmp_normalized/label_23.nii.gz has been save\n",
      "./tmp_normalized/image_24.nii.gz has been save\n",
      "./tmp_normalized/label_24.nii.gz has been save\n",
      "./tmp_normalized/image_25.nii.gz has been save\n",
      "./tmp_normalized/label_25.nii.gz has been save\n",
      "./tmp_normalized/image_26.nii.gz has been save\n",
      "./tmp_normalized/label_26.nii.gz has been save\n",
      "./tmp_normalized/image_27.nii.gz has been save\n",
      "./tmp_normalized/label_27.nii.gz has been save\n",
      "./tmp_normalized/image_28.nii.gz has been save\n",
      "./tmp_normalized/label_28.nii.gz has been save\n",
      "./tmp_normalized/image_29.nii.gz has been save\n",
      "./tmp_normalized/label_29.nii.gz has been save\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "scan_dir = OUT_FOLDER\n",
    "LIR = -125\n",
    "HIR = 275\n",
    "os.makedirs(scan_dir, exist_ok = True)\n",
    "\n",
    "reindex = 0\n",
    "for img_fid, seg_fid, pid in zip(imgs, segs, pids):\n",
    "\n",
    "    img_obj = sitk.ReadImage( img_fid )\n",
    "    seg_obj = sitk.ReadImage( seg_fid )\n",
    "\n",
    "    array = sitk.GetArrayFromImage(img_obj)\n",
    "\n",
    "    array[array > HIR] = HIR\n",
    "    array[array < LIR] = LIR\n",
    "    \n",
    "    array = (array - array.min()) / (array.max() - array.min()) * 255.0\n",
    "    \n",
    "    # then normalize this\n",
    "    \n",
    "    wined_img = sitk.GetImageFromArray(array)\n",
    "    wined_img = copy_spacing_ori(img_obj, wined_img)\n",
    "    \n",
    "    out_img_fid = os.path.join( scan_dir, f'image_{str(reindex)}.nii.gz' )\n",
    "    out_lb_fid  = os.path.join( scan_dir, f'label_{str(reindex)}.nii.gz' ) \n",
    "    \n",
    "    # then save\n",
    "    sitk.WriteImage(wined_img, out_img_fid, True) \n",
    "    sitk.WriteImage(seg_obj, out_lb_fid, True) \n",
    "    print(\"{} has been save\".format(out_img_fid))\n",
    "    print(\"{} has been save\".format(out_lb_fid))\n",
    "    reindex += 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
